package edu.njucm.herbiv.service

import edu.njucm.herbiv.dao.ChemicalRepository
import edu.njucm.herbiv.dao.FormulaRepository
import edu.njucm.herbiv.dao.ProteinRepository
import edu.njucm.herbiv.dao.TcmRepository
import edu.njucm.herbiv.pojo.entity.Chemical
import edu.njucm.herbiv.pojo.entity.Formula
import edu.njucm.herbiv.pojo.entity.Protein
import edu.njucm.herbiv.pojo.entity.Tcm
import edu.njucm.herbiv.utils.PageUtils.getPage
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service

/**
 * @Author ZhouHang
 * @Date 24/5/12 012
 */
@Service
class ChemicalService @Autowired constructor(
    private val chemicalRepository: ChemicalRepository
){
    fun list(type: String?, content: String?, page: Int, size: Int): Page<Chemical> {
        val searchContent = "%$content%"
        val pageable: Pageable = getPage(page, size, "id")
        return when (type) {
            "id" -> chemicalRepository.findAllByIdLike(searchContent, pageable)
            "name" -> chemicalRepository.findAllByNameLike(searchContent, pageable)
            "smiles" -> chemicalRepository.findAllBySmilesLike(searchContent, pageable)
            "casId" -> chemicalRepository.findAllByCasIdLike(searchContent, pageable)
            "pubChemId" -> chemicalRepository.findAllByCasIdLike(searchContent, pageable)
            "drugBankId" -> chemicalRepository.findAllByDrugBankIdLike(searchContent, pageable)
            "stitchId" -> chemicalRepository.findAllByStitchIdLike(searchContent, pageable)
            "herbId" -> chemicalRepository.findAllByHerbIdLike(searchContent, pageable)
            else -> chemicalRepository.findAll(pageable)
        }
    }
}


@Service
class ProteinService @Autowired constructor(
    private var proteinRepository: ProteinRepository
){
    fun list(type: String?, content: String?, page: Int, size: Int): Page<Protein> {
        val searchContent = "%$content%"
        val pageable: Pageable = getPage(page, size, "id")
        return when (type) {
            "id" -> proteinRepository.findAllByIdLike(searchContent, pageable)
            "proteinName" -> proteinRepository.findAllByProteinNameLike(searchContent, pageable)
            "geneName" -> proteinRepository.findAllByGeneNameLike(searchContent, pageable)
            else -> proteinRepository.findAll(pageable)
        }
    }
}


@Service
class TcmService @Autowired constructor(
    private val tcmRepository: TcmRepository
){
    fun list(type: String?, content: String?, page: Int, size: Int): Page<Tcm> {
        val searchContent = "%$content%"
        val pageable = getPage(page, size, "id")
        return when (type) {
            "id" -> tcmRepository.findAllByIdLike(searchContent, pageable)
            "cnName" -> tcmRepository.findAllByCnNameLike(searchContent, pageable)
            "pinyinName" -> tcmRepository.findAllByPinyinNameLike(searchContent, pageable)
            "enName" -> tcmRepository.findAllByEnNameLike(searchContent, pageable)
            "latinName" -> tcmRepository.findAllByLatinNameLike(searchContent, pageable)
            "properties" -> tcmRepository.findAllByPropertiesLike(searchContent, pageable)
            else -> tcmRepository.findAll(pageable)
        }
    }
}


@Service
class FormulaService @Autowired constructor(
    private val formulaRepository: FormulaRepository
){
    fun list(type: String?, content: String?, page: Int, size: Int): Page<Formula> {
        val searchContent = "%$content%"
        val pageable = getPage(page, size, "id")
        return when (type) {
            "id" -> formulaRepository.findAllByIdLike(searchContent, pageable)
            "name" -> formulaRepository.findAllByNameLike(searchContent, pageable)
            "instruction" -> formulaRepository.findAllByInstructionsLike(searchContent, pageable)
            "composition" -> formulaRepository.findAllByPrescriptionCompositionLike(searchContent, pageable)
            "symptom" -> formulaRepository.findAllByTreatmentSymptomsLike(searchContent, pageable)
            "source" -> formulaRepository.findAllBySourceDocumentLike(searchContent, pageable)
            else -> formulaRepository.findAll(pageable)
        }
    }
}

